﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceWarrantyRecordStatusService
    {
        #region 根据主键ID获取工单状态 + InsuranceWarrantyRecordStatus LoadByID(int id)
        public InsuranceWarrantyRecordStatus LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 根据状态码获取工单状态 + InsuranceWarrantyRecordStatus LoadByStatus(byte status)
        public InsuranceWarrantyRecordStatus LoadByStatus(byte status)
        {
            return LoadEntities(a => a.StatusCode == status).FirstOrDefault();
        }
        #endregion

        #region 判断状态原因是否存在 + bool IsExist(string statusName, int id = 0)
        /// <summary>
        /// 判断工单状态是否存在
        /// </summary>
        /// <param name="statusName">原因状态名称</param>
        /// <param name="id">状态码</param>
        /// <returns></returns>

        public bool IsExist(string statusName, int statusCode)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceWarrantyRecordStatus>();
            if (!string.IsNullOrWhiteSpace(statusName))
                expressionWhere = expressionWhere.And(o => o.StatusName.Equals(statusName));
            if (statusCode != 0)
                expressionWhere = expressionWhere.Or(o => o.StatusCode == statusCode);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 获取所有的状态 + IQueryable<InsuranceWarrantyRecordStatus> LoadAllEntity()
        public IQueryable<InsuranceWarrantyRecordStatus> LoadAllEntity()
        {
            //var expressionWhere = PredicateExtensions.True<InsuranceWarrantyRecordStatus>();
            return LoadEntities(t => true);
        }
        #endregion

        #region 获取下级工单状态 + IQueryable<InsuranceWarrantyRecordStatus> GetChildrenStatus(List<int> excludeCode, int parentId = 0)
        /// <summary>
        /// 获取下级工单状态
        /// </summary>
        /// <param name="excludeCode">需要排除掉的状态值</param>
        /// <param name="parentId">父级ID</param>
        /// <returns></returns>
        public IQueryable<InsuranceWarrantyRecordStatus> GetChildrenStatus(List<int> excludeCode, int parentId = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceWarrantyRecordStatus>();
            if (parentId > -1)
                expressionWhere = expressionWhere.And(t => t.ParentId == parentId);
            if (excludeCode != null)
                expressionWhere = expressionWhere.And(t => !excludeCode.Contains(t.StatusCode));
            return LoadEntities(expressionWhere);
        }
        #endregion
      #region 获取下级工单状态 + IQueryable<InsuranceWarrantyRecordStatus> GetChildrenStatus(byte parentCode, List<int> excludeCode)
        /// <summary>
        /// 获取下级工单状态
        /// </summary>
        /// <param name="parentCode">父级状态码Code</param>
        /// <param name="excludeCode">需要排除掉的状态值</param>
        /// <returns></returns>
        public IQueryable<InsuranceWarrantyRecordStatus> GetChildrenStatus(byte parentCode, List<int> excludeCode)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceWarrantyRecordStatus>();
            if (parentCode >= 0)
                expressionWhere = expressionWhere.And(t => t.ParentCode == parentCode);
            if (excludeCode != null)
                expressionWhere = expressionWhere.And(t => !excludeCode.Contains(t.StatusCode));
            return LoadEntities(expressionWhere);
        }
        #endregion

        #region 获取当前状态码的状态名称 + string GetStatusName(byte status)
        /// <summary>
        /// 获取当前状态码的状态名称
        /// </summary>
        /// <param name="status">当前状态码</param>
        /// <returns></returns>
        public string GetStatusName(byte status)
        {
            var item = LoadEntities(t => t.StatusCode == status).FirstOrDefault();
            if (item == null)
                return "";
            else
                return item.StatusName;
        }
        #endregion

        #region 获取上级状态码 + byte GetParentStatusCode(byte currentStatus)
        public byte GetParentStatusCode(byte currentStatus)
        {
          return  ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.LoadEntities(t => t.StatusCode == currentStatus).AsNoTracking().Select(t => t.ParentCode).FirstOrDefault();
        }
        #endregion
    }
}
